r"""
Author: dingcl
Date: 2025-03-07 16:23:27
LastEditTime: 2025-03-07 16:26:41
FilePath: \C++\unix_to_utc.py
Description:
write something here
"""

from datetime import datetime, timezone


def unix_to_utc(unix_timestamp):
    """
    将 Unix 时间戳转换为 UTC 时间字符串
    :param unix_timestamp: Unix 时间戳（整数或浮点数）
    :return: UTC 时间字符串，格式为 "YYYY-MM-DD HH:MM:SS"
    """
    return datetime.fromtimestamp(unix_timestamp, tz=timezone.utc).strftime(
        "%Y-%m-%d %H:%M:%S"
    )  # tz=timezone.utc表示使用UTC时间


def utc_to_unix(utc_time_str):
    """
    将 UTC 时间字符串转换为 Unix 时间戳
    :param utc_time_str: UTC 时间字符串，格式为 "YYYY-MM-DD HH:MM:SS"
    :return: Unix 时间戳（浮点数）
    """
    utc_time = datetime.strptime(utc_time_str, "%Y-%m-%d %H:%M:%S")
    return (utc_time - datetime(1970, 1, 1)).total_seconds()


# 测试示例
if __name__ == "__main__":
    # Unix 时间戳转 UTC 时间
    unix_timestamp = 1710892800
    utc_time = unix_to_utc(unix_timestamp)
    print(f"Unix 时间戳 {unix_timestamp} 对应的 UTC 时间为: {utc_time}")

    # UTC 时间转 Unix 时间戳
    utc_time_str = "2024-03-20 00:00:00"  # UTC 时间
    unix_timestamp = utc_to_unix(utc_time_str)
    print(f"UTC 时间 {utc_time_str} 对应的 Unix 时间戳为: {unix_timestamp}")
